package it.csi.finregio.finreges.client.services;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;

import it.crp.crparch.web.struts.service.CRPStrutsService;
import it.crp.crparch.web.struts.service.WebServiceException;
import it.crp.exception.ServiceException;
import it.crp.logger.PrdLoggerFactory;
import it.csi.finregio.finreges.client.ServiceLocator;
import it.csi.finregio.finreges.client.finders.FinderService;
import it.csi.finregio.finreges.dto.DestinatarioScheda;
import it.csi.finregio.finreges.dto.Scheda;
import it.csi.finregio.finreges.integration.SchedaService;
import it.csi.finregio.finreges.presentation.form.SchedaForm;

public class SalvaDestProcFin implements CRPStrutsService {

	private Logger log = PrdLoggerFactory.getInstance().getLogger("finreges");

	public Object execute(ActionForm form) throws WebServiceException {
		log.info("[SalvaDestProcFin::execute] BEGIN");
		try {
			SchedaForm sf = (SchedaForm) form;
			boolean insert = (sf.getDestinatari() != null && !sf
					.getDestinatari().trim().equals(""))
					|| (sf.getNoteDestinatari() != null && !sf
							.getNoteDestinatari().trim().equals(""))
					|| (sf.getProcedura() != null && !sf.getProcedura().trim()
							.equals(""))
					|| (sf.getNoteProcedura() != null && !sf.getNoteProcedura()
							.trim().equals(""))
					|| (sf.getLinkAssEnte() != null && !sf.getLinkAssEnte()
							.trim().equals(""))
					|| (sf.getFinanziamenti() != null && !sf.getFinanziamenti()
							.trim().equals(""))
					|| (sf.getNoteFinanziamenti() != null && !sf
							.getNoteFinanziamenti().trim().equals(""));

			if (!insert) {
				return sf;
			}
			DestinatarioScheda ds = new DestinatarioScheda();
			ds.setDestinatari(sf.getDestinatari());
			ds.setFinanziamenti(sf.getFinanziamenti());
			ds.setProcedura(sf.getProcedura());
			ds.setNoteDestinatari(sf.getNoteDestinatari());
			ds.setNoteFinanziamenti(sf.getNoteFinanziamenti());
			ds.setNoteProcedura(sf.getNoteProcedura());
			ds.setLinkAssEnte(sf.getLinkAssEnte());
			if (sf.getIdDestinatario() != null
					&& !sf.getIdDestinatario().trim().equals("")) {
				ds.setIdDestinatario(sf.getIdDestinatario());
			}
			SchedaService ss = new ServiceLocator().getSchedaService();
			Scheda scheda = ss.findSchedaByIdScheda(sf.getIdScheda());
			
			ds.setScheda(scheda);
			boolean changeStato = false;
			if(!sf.getStatoScheda().equals(scheda.getStato().getIdStato())){
				scheda.getStato().setIdStato(sf.getStatoScheda());
				changeStato=true;
				
			}
			if (ds.getIdDestinatario() == null
					|| ds.getIdDestinatario().trim().equals("")) {
				ds = ss.createDestinatarioScheda(ds);
			} else {
				ds = ss.aggiornaDestinatariSchede(ds);
			}
			sf.setIdDestinatario(ds.getIdDestinatario());
			if(changeStato){
				ss.aggiornaScheda(scheda);
			}
			return sf;
		} catch (ServiceException e) {
			log.error("[SalvaDestProcFin::execute]  " + e.getMessage(), e);
			throw new WebServiceException(e.getMessage());
		} catch (Exception e) {
			log.error("[SalvaDestProcFin::execute]  " + e.getMessage(), e);
			throw new WebServiceException(e.getMessage());
		} finally {
			log.info("[SalvaDestProcFin::execute] END");
		}
	}

}
